package com.scoressystem.controller;


import com.scoressystem.pojo.Score;
import com.scoressystem.service.ScoreService;
import com.scoressystem.utils.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/score")
public class ScoreController {
        @Autowired
        ScoreService scoreService;
        @RequestMapping("/getAllScore")
        public Results<List<Score>> getAllStudentPage(@RequestParam(value="stuName",defaultValue = "")String stuName,
                                                        @RequestParam(value = "page",defaultValue = "1") int pageIndex,
                                                        @RequestParam(value = "limit",defaultValue = "5") int pageSize) {
                Map<String,Object> map = new HashMap<>();
                map.put("stuName", "%"+stuName+"%");
                map.put("startPage", (pageIndex-1)*pageSize);
                map.put("pageSize", pageSize);
                List<Score> scores=scoreService.selectAllScore(map);
                long total=scoreService.selectAllScoreCount(map);
                Results<List<Score>> results=new Results("0","查询成功",scores,total);
                return results;

        }
        @RequestMapping("/addScore")
        public Results addSubject(Score score) {

                int count = scoreService.selectScoreByNameAndscore(score);
                System.out.println(score.getStuId()+score.getSubId());
                if (count>0) {
                        Results results = new Results("1", "新增失败，该学生此课程已存在");
                        return results;

                } else {
                        System.out.println(score);
                        scoreService.insertScore(score);
                        Results results = new Results("0", "新增成功");
                        return results;
                }
        }
        @RequestMapping("/updateScore")
        @ResponseBody
        public Results updateScore(Score score) {
                int count1 = scoreService.updateScore1(score);
                if (count1 > 0) {
                        Results results=new Results("0","更新成功");
                        return results;
                }
                else {
                        Results results=new Results("1","更新失败");
                        return results;

                }
        }
        @RequestMapping("/deleteScore")
        @ResponseBody
        public Results deleteScore(Score score) {
                int count=  scoreService.deleteById(score);
                if (count > 0) {
                        Results results=new Results("0","删除成功");
                        return results;
                }
                else {
                        Results results=new Results("1","删除失败");
                        return results;

                }

        }
        @RequestMapping("/getMyScores")
        @ResponseBody
        public Results<List<Score>> getAllStudentByNamePage(
                @RequestParam(value = "page",defaultValue = "1") int pageIndex,
                @RequestParam(value = "limit",defaultValue = "5") int pageSize, HttpSession session) {
                Map<String,Object> map = new HashMap<>();
                String studentNo= String.valueOf(session.getAttribute("stuNo"));
                map.put("startPage", (pageIndex-1)*pageSize);
                map.put("pageSize", pageSize);
                map.put("stuNo", studentNo);
                List<Score> scores=scoreService.selectAllScoreByNo(map);
                long total=scoreService.selectAllcountScoreByNo(map);
                Results<List<Score>> results=new Results("0","查询成功",scores,total);
                return results;

        }


}
